######################
# Global settings    #
######################
calibration_version = 20191107_nochoice
knitro_path = 'PATH_TO_KNITRO'  # set the path to your knitro version
drop_stop = 0.7  # should be 0.7, for testing set to 0.01 to compute everything in acceptable time

######################
# Directory settings #
######################

dir_project = .
dir_matlab = $(dir_project)/src/matlab
dir_densities = $(dir_matlab)/Densities
dir_r = $(dir_project)/src/r
dir_calibration = $(dir_matlab)/Calibration
dir_data_raw = $(dir_project)/data/raw
dir_data_generated = $(dir_project)/data/generated
dir_simulation = $(dir_data_generated)/matlab/simulation
dir_optimtax = $(dir_data_generated)/matlab/OptimTax
dir_optimtax_gpops = $(dir_optimtax)/gpops
dir_optimtax_hsv = $(dir_optimtax)/hsv
dir_compensation_hsv = $(dir_optimtax)/compensation_hsv
dir_compensation_gpops = $(dir_optimtax)/compensation_gpops
dir_calibration_output = $(dir_data_generated)/matlab/Calibration
dir_figures = $(dir_project)/figures
dir_doc_tables = $(dir_project)/tables

##########################
# Dependency collections #
##########################
tools = $(dir_matlab)/+tools/ChangeOfVariables.m $(dir_matlab)/+tools/csv2struct.m $(dir_matlab)/+tools/struct2csv.m $(dir_matlab)/+tools/Integration.m $(dir_matlab)/+tools/translators.m

densities = $(dir_densities)/ParticipationNormal.m  $(dir_densities)/SkillAndParticipation.m $(dir_densities)/abilityDistFixed.m $(dir_densities)/plogncdf.m $(dir_densities)/plognpdf.m $(dir_densities)/std_norminv.m

######################
# Target collections #
######################

prepare_cps_targets = $(dir_data_generated)/r/morg_data_stacked_90.csv
prepare_cps_dependencies = $(dir_r)/cps_functions.R $(dir_r)/prepare_cps.R $(dir_data_raw)/daviddorn/occ1970_occ1990dd.dta $(dir_data_raw)/daviddorn/occ1980_occ1990dd.dta $(dir_data_raw)/daviddorn/occ1990_occ1990dd.dta $(dir_data_raw)/daviddorn/occ2000_occ1990dd.dta $(dir_data_generated)/crosswalks/occ2012_occ1990.csv $(dir_data_generated)/crosswalks/occ00_occ1990.csv $(dir_data_raw)/AcemogluAutor2011/census-prep-files/occ1990dd-recode.csv $(dir_data_raw)/fred/DPCERG3A086NBEA.csv $(dir_data_raw)/morg/morg90.dta

sumstats_targets = $(dir_data_generated)/r/morg_sumstats_table.csv $(dir_data_generated)/r/morg_sumstats_all_table.csv $(dir_data_generated)/r/part_share.csv $(dir_data_generated)/r/mean_inc.csv
sumstats_dependencies = $(dir_r)/sumstats.R $(prepare_cps_targets)

prepare_skill_dist_calibration_targets = $(dir_data_generated)/r/freq_pareto_10_M.csv $(dir_data_generated)/r/freq_pareto_10_R.csv $(dir_data_generated)/r/freq_pareto_10_C.csv $(dir_data_generated)/r/wagebins_pareto_10_M.csv $(dir_data_generated)/r/wagebins_pareto_10_R.csv $(dir_data_generated)/r/wagebins_pareto_10_C.csv 
prepare_skill_dist_calibration_dependencies = $(dir_r)/prepare_skill_distribution_targets.R $(sumstats_targets) $(dir_data_raw)/constants.csv

calibrate_skill_dist_targets = $(dir_data_generated)/matlab/Calibration/$(calibration_version)_abilityDistThreeFixC_sol.csv $(dir_data_generated)/matlab/Calibration/$(calibration_version)_abilityDistThreeFixC_mom.csv $(dir_data_generated)/matlab/Calibration/$(calibration_version)_results.mat
calibrate_skill_dist_dependencies = $(dir_matlab)/Calibration/calibrate_abilityFixed.m  $(dir_matlab)/Densities/ParticipationNormal.m $(dir_matlab)/Densities/abilityDistFixed.m  $(dir_matlab)/Densities/SkillAndParticipation.m $(dir_matlab)/Densities/plognpdf.m $(dir_matlab)/Densities/plogncdf.m $(dir_matlab)/Densities/std_norminv.m $(dir_matlab)/UtilityFunctions/UtilityQuasiLinear.m  $(dir_matlab)/Calibration/CalibrateKrusellWithRobotsEconomy.m $(dir_matlab)/Calibration/CalibrateKrusellWithRobotsEconomyKnitro_con.opt $(tools) $(prepare_skill_dist_calibration_targets) $(dir_data_raw)/AcemogluRestrepo2018/dec_eps_AR2018.csv $(dir_data_generated)/r/part_share.csv $(dir_data_raw)/constants.csv $(dir_data_generated)/r/mean_inc.csv

calibrate_skill_dist_post_targets = $(dir_data_generated)/matlab/Calibration/$(calibration_version)_conditional_densities.csv $(dir_data_generated)/matlab/Calibration/$(calibration_version)_deciles_eps.csv
calibrate_skill_dist_post_dependencies = $(dir_matlab)/Calibration/calibrate_skill_dist_post.m $(calibrate_skill_dist_targets)

plots_skill_distribution_targets = $(dir_figures)/calibration/plot_densities_combined.pdf $(dir_figures)/calibration/deciles_eps.pdf
plot_skill_distribution_dependencies = $(dir_r)/plot_density_vs_data_FixC.R $(dir_r)/figures.R $(dir_data_generated)/r/morg_data_stacked_90.csv  $(calibrate_skill_dist_post_targets)

calibrate_prod_fun_targets = $(dir_data_generated)/matlab/Calibration/$(calibration_version)_KrusellRobotsEach_sol.csv $(dir_data_generated)/matlab/Calibration/$(calibration_version)_KrusellRobotsEach_mom.csv
calibrate_prod_fun_dependencies = $(calibrate_skill_dist_targets) $(dir_matlab)/Calibration/CalibrateKrusellWithRobotsEachEconomy.m $(dir_matlab)/Calibration/calibrate_production_function.m  $(densities) $(dir_matlab)/Economies/KrusellWithRobotsEachEconomy.m $(dir_matlab)/ProductionFunctions/KrusellRobotsEach.m $(dir_matlab)/UtilityFunctions/UtilityQuasiLinear.m $(tools) $(dir_matlab)/Calibration/production_function_calibration.opt $(dir_data_raw)/constants.csv

calibrate_inequity_aversion_targets = $(dir_data_generated)/matlab/Calibration/inequ_par.csv
calibrate_inequity_aversion_dependencies = $(calibrate_prod_fun_targets) $(calibrate_skill_dist_targets) $(dir_matlab)/OptimTax/calibrate_inequity_aversion.m $(dir_matlab)/Economies/OptimTaxKnitro.opt $(dir_data_raw)/constants.csv $(densities) $(dir_matlab)/UtilityFunctions/UtilityQuasiLinear.m $(dir_matlab)/ProductionFunctions/KrusellRobotsEach.m $(dir_matlab)/Economies/KrusellWithRobotsEachEconomy.m

table_targets = $(dir_doc_tables)/out/master_tables.tex $(dir_doc_tables)/out/tab_cal_ability_FixC.tex $(dir_doc_tables)/out/tab_cal_moments_combined.tex $(dir_doc_tables)/out/tab_morg_sumstats.tex $(dir_doc_tables)/out/tab_cal_prod_fun.tex

table_dependencies = $(dir_doc_tables)/master_tables.lyx $(dir_doc_tables)/tab_cal_moments_combined.lyx $(dir_doc_tables)/tab_cal_ability_FixC.lyx  $(dir_doc_tables)/tab_morg_sumstats.lyx $(dir_doc_tables)/tab_cal_prod_fun.lyx   $(dir_doc_tables)/tab_occ_classification.lyx  $(dir_doc_tables)/tab_cal_moments.R $(dir_doc_tables)/tab_cal_ability_FixC.R $(dir_doc_tables)/tab_morg_sumstats.R $(sumstats_targets) $(dir_doc_tables)/tab_cal_prod_fun.R  $(dir_data_generated)/matlab/Calibration/$(calibration_version)_abilityDistThreeFixC_mom.csv  $(dir_data_generated)/matlab/Calibration/$(calibration_version)_KrusellRobotsEach_sol.csv calibration_version.txt $(calibrate_inequity_aversion_targets) $(dir_doc_tables)/funcs_and_cons.R $(dir_data_raw)/constants.csv $(dir_data_generated)/r/part_share.csv $(dir_data_generated)/r/mean_inc.csv

simulate_given_tax_targets =  $(dir_simulation)/$(calibration_version)_simulation.csv  $(dir_simulation)/$(calibration_version)_simulation_high_tax.csv
simulate_given_tax_dependencies = $(dir_matlab)/Calibration/equilibrium_price_change.m $(densities) $(calibrate_skill_dist_targets) $(calibrate_prod_fun_targets) $(dir_matlab)/Economies/KrusellWithRobotsEachEconomy.m $(dir_matlab)/Economies/ComputeEquilibriumKnitro.opt $(dir_matlab)/ProductionFunctions/KrusellRobotsEach.m $(dir_matlab)/UtilityFunctions/UtilityQuasiLinear.m $(tools)

# gpops
optimtax_gpops_optimum_targets = $(dir_optimtax_gpops)/optimum_vars.csv $(dir_optimtax_gpops)/optimum_vectors.csv 
optimtax_gpops_optimum_dependencies = $(dir_matlab)/GPOPS/GPOPSMain.m $(dir_matlab)/GPOPS/GPOPSContinuous.m $(dir_matlab)/GPOPS/GPOPSEndpoint.m $(dir_matlab)/GPOPS/GPOPS_plot_results.m $(dir_matlab)/GPOPS/GPOPSFunctions.m $(dir_matlab)/GPOPS/ProductionFunction.m $(dir_matlab)/GPOPS/SkillDistribution.m $(dir_matlab)/GPOPS/ParticipationCostDistribution.m $(dir_matlab)/GPOPS/UtilityFunction.m $(dir_matlab)/GPOPS/fit_plognpdf.m $(dir_matlab)/GPOPS/compute_elasticities.m $(dir_matlab)/GPOPS/keepfield.m $(calibrate_skill_dist_targets) $(calibrate_prod_fun_targets) $(dir_matlab)/Economies/KrusellWithRobotsEachEconomy.m $(dir_matlab)/ProductionFunctions/KrusellRobotsEach.m $(dir_matlab)/UtilityFunctions/UtilityQuasiLinear.m $(tools) $(calibrate_inequity_aversion_targets)

optimtax_gpops_default_capital_tax_targets = $(dir_optimtax_gpops)/default_capital_tax_vars.csv $(dir_optimtax_gpops)/default_capital_tax_vectors.csv
optimtax_gpops_default_capital_tax_dependencies = $(optimtax_gpops_optimum_dependencies) $(optimtax_gpops_optimum_targets)

optimtax_gpops_equipment_equal_robot_tax_targets = $(dir_optimtax_gpops)/equipment_equal_robot_tax_vars.csv $(dir_optimtax_gpops)/equipment_equal_robot_tax_vectors.csv
optimtax_gpops_equipment_equal_robot_tax_dependencies = $(optimtax_gpops_optimum_dependencies) $(optimtax_gpops_optimum_targets)

# optimtax_hsv (depends on optimtax_gpops_optimum_targets since wage bounds read from there)
optimtax_hsv_dependencies =  $(densities) $(calibrate_skill_dist_targets) $(calibrate_prod_fun_targets) $(dir_matlab)/Economies/KrusellWithRobotsEachEconomy.m $(dir_matlab)/ProductionFunctions/KrusellRobotsEach.m $(dir_matlab)/UtilityFunctions/UtilityQuasiLinear.m $(tools) $(optimtax_gpops_optimum_targets) $(dir_matlab)/OptimTax/optimtax_hsv.m $(dir_matlab)/Economies/OptimTaxKnitro.opt $(calibrate_inequity_aversion_targets)

optimtax_hsv_adjust_tau_hsv_targets = $(dir_optimtax_hsv)/$(calibration_version)_adjust_tau_hsv.csv
optimtax_hsv_adjust_tau_hsv_dependencies = $(optimtax_hsv_dependencies)

optimtax_hsv_adjust_tau_B_targets = $(dir_optimtax_hsv)/$(calibration_version)_adjust_robot_tax.csv
optimtax_hsv_adjust_tau_B_dependencies = $(optimtax_hsv_dependencies)

optimtax_hsv_adjust_taus_targets = $(dir_optimtax_hsv)/$(calibration_version)_adjust_taus.csv
optimtax_hsv_adjust_taus_dependencies = $(optimtax_hsv_dependencies)

compensate_gpops_default_capital_tax_targets = $(dir_compensation_gpops)/$(calibration_version)_compensation_baseline_default_capital_tax.csv
compensate_gpops_default_capital_tax_dependencies = $(optimtax_hsv_dependencies) $(optimtax_hsv_adjust_tau_hsv_targets) $(optimtax_gpops_default_capital_tax_targets) 

compensate_gpops_equipment_equal_robot_tax_targets = $(dir_compensation_gpops)/$(calibration_version)_compensation_baseline_equipment_equal_robot_tax.csv
compensate_gpops_equipment_equal_robot_tax_dependencies = $(optimtax_hsv_dependencies) $(optimtax_hsv_adjust_tau_hsv_targets) $(optimtax_gpops_equipment_equal_robot_tax_targets)

compensate_gpops_optimum_targets = $(dir_compensation_gpops)/$(calibration_version)_compensation_baseline_optimum.csv
compensate_gpops_optimum_dependencies = $(optimtax_hsv_dependencies) $(optimtax_hsv_adjust_tau_hsv_targets)  $(optimtax_gpops_optimum_targets)

compensate_optimal_robot_tax_hsv_targets = $(dir_compensation_hsv)/$(calibration_version)_compensation_baseline_optimal_robot_tax_hsv.csv
compensate_optimal_robot_tax_hsv_dependencies = $(optimtax_hsv_dependencies) $(optimtax_hsv_adjust_tau_B_targets)

compensate_optimtax_hsv_adjust_taus_targets = $(dir_compensation_hsv)/$(calibration_version)_compensation_baseline_optimtax_hsv_adjust_taus.csv
compensate_optimtax_hsv_adjust_taus_dependencies = $(optimtax_hsv_dependencies) $(optimtax_hsv_adjust_taus_targets)

sensitivity_targets = $(dir_simulation)/$(calibration_version)_moments_sensitivity.csv
sensitivity_dependencies = $(simulate_given_tax_dependencies) $(dir_calibration)/analyze_sensitivity.m

# robustness wrt. kappa & gamma - runs for 10 days
sensitivity_kappa_gamma_targets = $(dir_optimtax_gpops)/sensitivity_vars_kappa_incr.csv $(dir_optimtax_gpops)/sensitivity_vars_kappa_decr.csv
sensitivity_kappa_gamma_dependencies = $(dir_matlab)/GPOPS/GPOPSRobustness.m $(dir_matlab)/GPOPS/GPOPSContinuous.m $(dir_matlab)/GPOPS/GPOPSEndpoint.m $(dir_matlab)/GPOPS/GPOPS_plot_results.m $(dir_matlab)/GPOPS/GPOPSFunctions.m $(dir_matlab)/GPOPS/ProductionFunction.m $(dir_matlab)/GPOPS/SkillDistribution.m $(dir_matlab)/GPOPS/ParticipationCostDistribution.m $(dir_matlab)/GPOPS/UtilityFunction.m $(dir_matlab)/GPOPS/fit_plognpdf.m $(dir_matlab)/GPOPS/compute_elasticities.m $(dir_matlab)/GPOPS/keepfield.m $(calibrate_skill_dist_targets) $(calibrate_prod_fun_targets) $(dir_matlab)/Economies/KrusellWithRobotsEachEconomy.m $(dir_matlab)/ProductionFunctions/KrusellRobotsEach.m $(dir_matlab)/UtilityFunctions/UtilityQuasiLinear.m $(tools) $(calibrate_inequity_aversion_targets)

# computations based on simple formula
compute_tax_simple_formula_targets = $(dir_data_generated)/matlab/robustness/EPS_W_M.csv $(dir_data_generated)/matlab/robustness/EPS_W_C.csv $(dir_data_generated)/matlab/robustness/TAU_B_EPS.csv  $(dir_data_generated)/matlab/robustness/PSI_M.csv $(dir_data_generated)/matlab/robustness/PSI_C.csv $(dir_data_generated)/matlab/robustness/TAU_B_PSI.csv
compute_tax_simple_formula_dependencies = $(dir_matlab)/SimpleFormula/plot_simple_tax_formula.m $(dir_matlab)/SimpleFormula/simple_tax_formula.m $(dir_simulation)/$(calibration_version)_simulation_fix_capital.csv $(dir_data_raw)/constants.csv

# plots
plots_simulate_given_tax_targets = $(dir_figures)/simulation/low_high_tax_legend.pdf $(dir_figures)/simulation/plots_simulate_given_tax_combined_top.pdf  $(dir_figures)/simulation/legend_three_mrc.pdf $(dir_figures)/simulation/plots_simulate_given_tax_combined_bottom.pdf
plots_simulate_given_tax_dependencies = $(simulate_given_tax_targets) $(dir_r)/produce_figures_simulate_given_tax.R $(dir_r)/figures.R

plots_gpops_targets = $(dir_figures)/GPOPS/legend_tax_compare.pdf $(dir_figures)/GPOPS/gpops_tax_compare_combined.pdf $(dir_figures)/GPOPS/gpops_plots_combined.pdf 
plots_gpops_dependencies = $(dir_r)/figures.R $(dir_r)/produce_figures_gpops.R $(optimtax_gpops_optimum_targets)

plots_compensation_targets = $(dir_figures)/compensation/compensation_combined.pdf $(dir_figures)/compensation/compensation_tau_B.pdf $(dir_figures)/compensation/compensation_taus.pdf
plots_compensation_dependencies = $(dir_r)/figures.R $(dir_r)/produce_figures_compensation.R $(dir_compensation_gpops)/$(calibration_version)_compensation_baseline_optimum.csv $(dir_compensation_gpops)/$(calibration_version)_compensation_baseline_default_capital_tax.csv $(dir_compensation_gpops)/$(calibration_version)_compensation_baseline_equipment_equal_robot_tax.csv

plots_gains_losses_targets =  $(dir_figures)/compensation/plot_gains_losses_welfare.pdf $(dir_figures)/compensation/plot_gains_losses_participation.pdf
plots_gains_losses_dependencies = $(simulate_given_tax_targets) $(optimtax_hsv_adjust_tau_B_targets) $(optimtax_gpops_optimum_targets) $(dir_r)/figures.R $(dir_r)/gains_losses.R

plots_formula_terms_targets = $(dir_figures)/decomposition/formula_terms_combined.pdf $(dir_figures)/decomposition/tax_rev_combined.pdf $(dir_figures)/decomposition/formula_decomposition.pdf $(dir_figures)/decomposition/legend_formula_decomposition.pdf
plots_formula_terms_dependencies = $(dir_r)/figures.R $(dir_r)/produce_figures_formula_terms.R $(dir_optimtax_gpops)/optimum_vars.csv

plots_sensitivity_targets = $(dir_figures)/sensitivity/moments_sensitivity.pdf
plots_sensitivity_dependencies = $(dir_r)/figures.R $(dir_r)/moments_sensitivity.R $(sensitivity_targets)

plots_sensitivity_kappa_gamma_targets = $(dir_figures)/sensitivity/robustness_kappa_gamma.pdf
plots_sensitivity_kappa_gamma_dependencies = $(dir_r)/plot_sensitivity_kappa_gamma.R $(sensitivity_kappa_gamma_targets)

plots_boxplot_targets = $(dir_figures)/data_appendix/boxplot_2digit.pdf
plots_boxplot_dependencies = $(dir_r)/data_appendix.R $(dir_data_generated)/r/morg_data_stacked_90.csv

plots_simple_formula_targets = $(dir_figures)/robustness/plots_simple_formula_combined.pdf
plots_simple_formula_dependencies = $(dir_r)/plot_simple_formula.R $(compute_tax_simple_formula_targets)

#################
# Targets  #
#################

# Collections
all: prepare_cps sumstats prepare_skill_dist_calibration calibrate_skill_dist calibrate_skill_dist_post calibrate_prod_fun calibrate_inequity_aversion tables simulate_given_tax optimtax_gpops optimtax_hsv_adjust_tau_hsv optimtax_hsv_adjust_tau_B optimtax_hsv_adjust_taus compensate_optimal_robot_tax_hsv compensate_optimtax_hsv_adjust_taus compensate_gpops analyze_sensitivity compute_tax_simple_formula plots sensitivity_kappa_gamma plots_sensitivity_kappa_gamma

optimtax_gpops: optimtax_gpops_optimum optimtax_gpops_default_capital_tax optimtax_gpops_equipment_equal_robot_tax
compensate_gpops: compensate_gpops_default_capital_tax compensate_gpops_equipment_equal_robot_tax compensate_gpops_optimum

plots: plots_skill_distribution_calibration plots_simulate_given_tax plots_gpops plots_compensation plots_gains_losses plots_formula_terms plots_sensitivity  plots_simple_formula plots_boxplot

clean:
	rm -fv $(dir_data_generated)/r/*.*
	rm -fv $(dir_data_generated)/matlab/Calibration/*.*
	rm -fv $(dir_data_generated)/matlab/simulation/*.*
	rm -fv $(dir_data_generated)/matlab/OptimTax/compensation_gpops/*.*
	rm -fv $(dir_data_generated)/matlab/OptimTax/compensation_hsv/*.*
	rm -fv $(dir_data_generated)/matlab/OptimTax/hsv/*.*
	rm -fv $(dir_data_generated)/matlab/OptimTax/gpops/*.*
	rm -fv $(dir_figures)/*.*
	rm -fv $(dir_figures)/compensation/*.*
	rm -fv $(dir_figures)/data_appendix/*.*
	rm -fv $(dir_figures)/decomposition/*.*
	rm -fv $(dir_figures)/GPOPS/*.*
	rm -fv $(dir_figures)/simulation/*.*
	rm -fv $(dir_figures)/calibration/*.*
	rm -fv $(dir_doc_tables)/out/*.tex

# Single recipes
prepare_cps: $(prepare_cps_targets)
prepare_skill_dist_calibration: $(prepare_skill_dist_calibration_targets)
calibrate: calibrate_skill_dist calibrate_skill_dist_post calibrate_prod_fun 
calibrate_skill_dist: $(calibrate_skill_dist_targets)
calibrate_prod_fun: $(calibrate_prod_fun_targets)
calibrate_skill_dist_post: $(calibrate_skill_dist_post_targets)
calibrate_inequity_aversion: $(calibrate_inequity_aversion_targets)
plots_skill_distribution_calibration: $(plots_skill_distribution_targets)
simulate_given_tax: $(simulate_given_tax_targets)

optimtax_gpops_optimum: $(optimtax_gpops_optimum_targets)
optimtax_gpops_default_capital_tax: $(optimtax_gpops_default_capital_tax_targets)
optimtax_gpops_equipment_equal_robot_tax: $(optimtax_gpops_equipment_equal_robot_tax_targets)

optimtax_hsv_adjust_tau_hsv: $(optimtax_hsv_adjust_tau_hsv_targets)
optimtax_hsv_adjust_tau_B: $(optimtax_hsv_adjust_tau_B_targets)
optimtax_hsv_adjust_taus: $(optimtax_hsv_adjust_taus_targets)

compensate_gpops_default_capital_tax: $(compensate_gpops_default_capital_tax_targets)
compensate_gpops_equipment_equal_robot_tax: $(compensate_gpops_equipment_equal_robot_tax_targets)
compensate_gpops_optimum: $(compensate_gpops_optimum_targets)
compensate_optimal_robot_tax_hsv: $(compensate_optimal_robot_tax_hsv_targets)
compensate_optimtax_hsv_adjust_taus: $(compensate_optimtax_hsv_adjust_taus_targets)

analyze_sensitivity: $(sensitivity_targets)
sensitivity_kappa_gamma: $(sensitivity_kappa_gamma_targets)  # runs for 10 days (can run incr and decr in parallel, to reduce to 5 days)

compute_tax_simple_formula: $(compute_tax_simple_formula_targets)

plots_simulate_given_tax: $(plots_simulate_given_tax_targets)
plots_gpops: $(plots_gpops_targets)
plots_gains_losses: $(plots_gains_losses_targets)
plots_compensation: $(plots_compensation_targets)
plots_formula_terms: $(plots_formula_terms_targets)
plots_sensitivity: $(plots_sensitivity_targets)
plots_sensitivity_kappa_gamma: $(plots_sensitivity_kappa_gamma_targets)
plots_boxplot: $(plots_boxplot_targets)
plots_simple_formula: $(plots_simple_formula_targets)

sumstats: $(sumstats_targets)
tables: $(table_targets)

###########
# Recipes #
###########

calibration_version.txt:
	@echo "============================================================"
	@echo "Storing meta parameters"
	@echo "============================================================"
	echo "$(calibration_version)" >> calibration_version.txt

$(optimtax_hsv_adjust_tau_hsv_targets): $(optimtax_hsv_adjust_tau_hsv_dependencies)
	@echo "============================================================"
	@echo "Optimal taxes under HSV system - HSV system given capital taxes"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "wage_and_price_file='../../.$(dir_optimtax_gpops)/optimum_vars.csv';cd '$(dir_matlab)/OptimTax';optimtax_hsv('adjust_tau_hsv',$(knitro_path),'wage_and_price_file',wage_and_price_file,'drop_stop',0);exit"

$(optimtax_hsv_adjust_tau_B_targets): $(optimtax_hsv_adjust_tau_B_dependencies)
	@echo "============================================================"
	@echo "Optimal robot tax under HSV system with fixed progressivity"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r ";init_file='../../.$(dir_optimtax_hsv)/$(calibration_version)_adjust_tau_hsv.csv';wage_and_price_file='../../.$(dir_optimtax_gpops)/optimum_vars.csv';cd '$(dir_matlab)/OptimTax';optimtax_hsv('adjust_robot_tax',$(knitro_path),'wage_and_price_file',wage_and_price_file,'compensation_init_file',init_file);exit"

$(optimtax_hsv_adjust_taus_targets): $(optimtax_hsv_adjust_taus_dependencies)
	@echo "============================================================"
	@echo "Optimal capital taxes under HSV system with fixed progressivity"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r ";init_file='../../.$(dir_optimtax_hsv)/$(calibration_version)_adjust_tau_hsv.csv';wage_and_price_file='../../.$(dir_optimtax_gpops)/optimum_vars.csv';cd '$(dir_matlab)/OptimTax';optimtax_hsv('adjust_taus',$(knitro_path),'wage_and_price_file',wage_and_price_file,'compensation_init_file',init_file);exit"

# init file is used to set tau_hsv when computing compensation
$(compensate_gpops_default_capital_tax_targets): $(compensate_gpops_default_capital_tax_dependencies)
	@echo "============================================================"
	@echo "Compensation in HSV wrt. GPOPS default capital tax"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_matlab)/OptimTax';init_file='../../.$(dir_optimtax_hsv)/$(calibration_version)_adjust_tau_hsv.csv';targets_file='../../.$(dir_optimtax_gpops)/default_capital_tax_vars.csv';optimtax_hsv('baseline',$(knitro_path),'compute_compensation',true,'welfare_targets_file',targets_file,'suffix','default_capital_tax','compensation_init_file',init_file);exit"

$(compensate_gpops_equipment_equal_robot_tax_targets): $(compensate_gpops_equipment_equal_robot_tax_dependencies)
	@echo "============================================================"
	@echo "Compensation in HSV wrt. GPOPS tau_E = tau_B"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_matlab)/OptimTax';init_file='../../.$(dir_optimtax_hsv)/$(calibration_version)_adjust_tau_hsv.csv';targets_file='../../.$(dir_optimtax_gpops)/equipment_equal_robot_tax_vars.csv';optimtax_hsv('baseline',$(knitro_path),'compute_compensation',true,'welfare_targets_file',targets_file,'suffix','equipment_equal_robot_tax','compensation_init_file',init_file);exit"

$(compensate_gpops_optimum_targets): $(compensate_gpops_optimum_dependencies)
	@echo "============================================================"
	@echo "Compensation in HSV wrt. GPOPS optimum"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_matlab)/OptimTax';init_file='../../.$(dir_optimtax_hsv)/$(calibration_version)_adjust_tau_hsv.csv';targets_file='../../.$(dir_optimtax_gpops)/optimum_vars.csv';optimtax_hsv('baseline',$(knitro_path),'compute_compensation',true,'welfare_targets_file',targets_file,'suffix','optimum','compensation_init_file',init_file);exit"

$(compensate_optimal_robot_tax_hsv_targets): $(compensate_optimal_robot_tax_hsv_dependencies)
	@echo "============================================================"
	@echo "Compensation in HSV wrt. optimal robot tax under HSV"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_matlab)/OptimTax';init_file='../../.$(dir_optimtax_hsv)/$(calibration_version)_adjust_robot_tax.csv';targets_file=init_file;optimtax_hsv('baseline',$(knitro_path),'compute_compensation',true,'welfare_targets_file',targets_file,'suffix','optimal_robot_tax_hsv','compensation_init_file',init_file);exit"

$(compensate_optimtax_hsv_adjust_taus_targets): $(compensate_optimtax_hsv_adjust_taus_dependencies)
	@echo "============================================================"
	@echo "Compensation in HSV wrt. optimal capital taxes under HSV"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_matlab)/OptimTax';init_file='../../.$(dir_optimtax_hsv)/$(calibration_version)_adjust_taus.csv';targets_file=init_file;optimtax_hsv('baseline',$(knitro_path),'compute_compensation',true,'welfare_targets_file',targets_file,'suffix','optimtax_hsv_adjust_taus','compensation_init_file',init_file);exit"

$(sensitivity_targets): $(sensitivity_dependencies)
	@echo "============================================================"
	@echo "Analyzing sensitivity of prod fun parameters"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_calibration)'; analyze_sensitivity('$(calibration_version)','$(calibration_version)','$(calibration_version)',$(knitro_path),false);exit"

$(optimtax_gpops_optimum_targets): $(optimtax_gpops_optimum_dependencies)
	@echo "============================================================"
	@echo "Optimal taxes - full optimum"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_matlab)/GPOPS';GPOPSMain('calibration_version','$(calibration_version)','version','optimum','drop_stop',$(drop_stop),'drop_step',0.01);exit"

$(optimtax_gpops_default_capital_tax_targets): $(optimtax_gpops_default_capital_tax_dependencies)
	@echo "============================================================"
	@echo "Optimal taxes under default capital taxes"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_matlab)/GPOPS';GPOPSMain('calibration_version','$(calibration_version)','version','default_capital_tax','drop_stop',$(drop_stop),'drop_step',0.01);exit"

$(optimtax_gpops_equipment_equal_robot_tax_targets): $(optimtax_gpops_equipment_equal_robot_tax_dependencies)
	@echo "============================================================"
	@echo "Optimal taxes under equipment_tax = robot_tax"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_matlab)/GPOPS';GPOPSMain('calibration_version','$(calibration_version)','version','equipment_equal_robot_tax','drop_stop',$(drop_stop),'drop_step',0.01);exit"

$(simulate_given_tax_targets): $(simulate_given_tax_dependencies)
	@echo "============================================================"
	@echo "Simulating price change for given tax"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_calibration)';equilibrium_price_change('$(calibration_version)','$(calibration_version)','$(calibration_version)',$(drop_stop),$(knitro_path));exit"	

$(table_targets): $(table_dependencies)
	@echo "============================================================"
	@echo "Creating tables"
	@echo "============================================================"
	rm $(dir_doc_tables)/out/*.texo; lyx --export-to pdflatex "$(dir_doc_tables)/out/master_tables.tex" $(dir_doc_tables)/master_tables.lyx -f --force-overwrite

$(calibrate_inequity_aversion_targets): $(calibrate_inequity_aversion_dependencies)
	@echo "============================================================"
	@echo "Calibrating inequity aversion parameter"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_matlab)/OptimTax';calibrate_inequity_aversion([0.7:0.01:0.8],1,1e4,$(knitro_path))';exit"

$(calibrate_prod_fun_targets): $(calibrate_prod_fun_dependencies)
	@echo "============================================================"
	@echo "Calibrating production function"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_calibration)';calibrate_production_function('$(calibration_version)','$(calibration_version)',$(knitro_path))';exit"	

$(plots_skill_distribution_targets): $(plot_skill_distribution_dependencies)
	@echo "============================================================"
	@echo "Plots for skill distribution calibration"
	@echo "============================================================"
	Rscript src/r/plot_density_vs_data_FixC.R

$(calibrate_skill_dist_post_targets): $(calibrate_skill_dist_post_dependencies)
	@echo "============================================================"
	@echo "Post analysis - skill distribution calibration"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_calibration)';calibrate_skill_dist_post('$(calibration_version)')';exit"

$(calibrate_skill_dist_targets): $(calibrate_skill_dist_dependencies)
	@echo "============================================================"
	@echo "Skill distribution calibration"
	@echo "============================================================"
	@echo "Making $(calibrate_skill_dist_targets)"
	matlab -nodisplay -nosplash -r "version_skill_dist = '$(calibration_version)';cd '$(dir_calibration)';calibrate_abilityFixed(version_skill_dist, $(knitro_path))';exit"

$(prepare_skill_dist_calibration_targets): $(prepare_skill_dist_calibration_dependencies)
	@echo "============================================================"
	@echo "Preparing targets for skill distribution calibration"
	@echo "============================================================"
	Rscript src/r/prepare_skill_distribution_targets.R

$(compute_tax_simple_formula_targets): $(compute_tax_simple_formula_dependencies)
	@echo "============================================================"
	@echo "Computing tax based on simple formula"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_matlab)/SimpleFormula';plot_simple_tax_formula;exit"

$(sensitivity_kappa_gamma_targets): $(sensitivity_kappa_gamma_dependencies)
	@echo "============================================================"
	@echo "Optimal taxes for different values of gamma and kappa"
	@echo "============================================================"
	matlab -nodisplay -nosplash -r "cd '$(dir_matlab)/GPOPS';GPOPSRobustness('calibration_version',$(calibration_version),'version','sensitivity','kappa_R_increase',true,'num_points',50);GPOPSRobustness('calibration_version',$(calibration_version),'version','sensitivity','kappa_R_increase',false,'num_points',50);exit"

$(sumstats_targets): $(dir_r)/sumstats.R $(prepare_cps_targets)
	@echo "============================================================"
	@echo "Computing summary statistics"
	@echo "============================================================"
	Rscript src/r/sumstats.R

$(prepare_cps_targets): $(prepare_cps_dependencies)
	@echo "============================================================"
	@echo "Preparing morg data"
	@echo "============================================================"
	Rscript src/r/prepare_cps.R

$(plots_simulate_given_tax_targets): $(plots_simulate_given_tax_dependencies)
	@echo "============================================================"
	@echo "Figures for simulation given tax"
	@echo "============================================================"
	Rscript src/r/produce_figures_simulate_given_tax.R

$(plots_gpops_targets): $(plots_gpops_dependencies)
	@echo "============================================================"
	@echo "Figures for GPOPS optimal tax"
	@echo "============================================================"
	Rscript src/r/produce_figures_gpops.R

$(plots_compensation_targets): $(plots_compensation_dependencies)
	@echo "============================================================"
	@echo "Figures for welfare compensation"
	@echo "============================================================"
	Rscript src/r/produce_figures_compensation.R

$(plots_gains_losses_targets): $(plots_gains_losses_dependencies)
	@echo "============================================================"
	@echo "Figures for gains and losses"
	@echo "============================================================"
	Rscript src/r/gains_losses.R

$(plots_formula_terms_targets): $(plots_formula_terms_dependencies)
	@echo "============================================================"
	@echo "Figures for decomposition based on robot tax formula"
	@echo "============================================================"
	Rscript src/r/produce_figures_formula_terms.R

$(plots_sensitivity_targets): $(plots_sensitivity_dependencies)
	@echo "============================================================"
	@echo "Figures for sensitivity analysis"
	@echo "============================================================"
	Rscript src/r/moments_sensitivity.R

$(plots_sensitivity_kappa_gamma_targets): $(plots_sensitivity_kappa_gamma_dependencies)
	@echo "============================================================"
	@echo "Figures for sensitivity analysis wrt. kappa and gamma"
	@echo "============================================================"
	Rscript src/r/plot_sensitivity_kappa_gamma.R

$(plots_simple_formula_targets): $(plots_simple_formula_dependencies)
	@echo "============================================================"
	@echo "Figures for taxes based on simple formula"
	@echo "============================================================"
	Rscript src/r/plot_simple_formula.R

$(plots_boxplot_targets): $(plots_boxplot_dependencies)
	@echo "============================================================"
	@echo "Boxplot for Data Appendix"
	@echo "============================================================"
	Rscript src/r/data_appendix.R

